package com.taobao.snowsnow.taobao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.taobao.api.internal.util.WebUtils;

public class TaoBaoUtil {
	public final static String APP_KEY = "12383292";
	public final static String APP_SERCET = "2e161123af8987b7b63518089705d351";
	public final static String MYLEFT1_SESSIONKEY = "61022032dbd13cb6a657b94f9472a376a3eec8a6f28175c40592164";
	public final static String QINANHG_SESSIONKEY = "6101a047b01e11e2e19f3f52e29ceaf0919ad2875e49d0a161033820";
	public final static String SESSIONKEY = MYLEFT1_SESSIONKEY;
	
	public final static String TAOBAO_URL = "http://gw.api.tbsandbox.com/router/rest";
	public final static String TAOBAO_F_URL = "http://gw.api.taobao.com/router/rest";
	public final static String GET_SESSION_URL = "http://container.open.taobao.com/container?authcode=";
	
	/**
	 * 通过授权码来获取 sessionkey
	 * @param authcode 授权码
	 * @return sessionkey
	 */
	public static String getSessionKey(String authcode) {
		String url = GET_SESSION_URL + authcode;
		String result = "";
		
		try {
			result = WebUtils.doGet(url, null);
		} catch (IOException e) {
			System.out.println("获取sessionkey出错");
		}
		return result;
	}

	/**
	 * 得到返回的内容
	 */
	public static String getResult(String urlStr) {
		URL url = null;
		HttpURLConnection connection = null;

		try {
			url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("GET");
			connection.setUseCaches(false);
			connection.connect();

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection
					.getInputStream(), "utf-8"));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			reader.close();
			return buffer.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return null;
	}
	
    public static void main(String[] args) {
    	System.out.println(getResult("http://baidu.kuaidi100.com/query?type=shentong&postid=468562074627"));
//		System.out.println(getSessionKey(GET_SESSION_URL + "TOP-10b0e254c565bea3cf798ed8fb678635d7Cj6cOppamFOw3hRBEgcEi361xqOhXh-END"));
	}
}
